package com.team407.cheyoubang.down;

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;

public class DownInfo implements Callback{
	public String url;
	public long totalSize;
	public long stPos = 0;
	public long curPos= 0;
	public String filePath;
	public boolean isPause = false;
	
	private DownCallBack mDownCallBack;
	
	private Handler handler = new Handler(this);
	
	public void updateProgress(){
		if(mDownCallBack != null){
			handler.sendEmptyMessage(UPDATEPROGRESS);
		}
	}
	
	public void pause(){
		isPause = true;
	}
	
	public void setDownCallBack(DownCallBack downCallBack){
		mDownCallBack = downCallBack;
	}
	
	//准备下载
	public void prepare(){
		if(mDownCallBack != null){
			handler.sendEmptyMessage(PREPARE);
		}
	}
	
	//完成下载
	public void finlish(){
		if(mDownCallBack != null){
			handler.sendEmptyMessage(FINLISH);
		}
	}
	//网络异常
	public void error(){
		if(mDownCallBack != null){
			handler.sendEmptyMessage(ERROR);
		}
	}
	
	public interface DownCallBack{
		void prepare(String url, long totalSize);
		void error(String url);
		void finlish(String url, boolean isPause);
		void updateProgress(String url, long curPos, long totalSize);
	}
	
	public static final int PREPARE = 2;
	public static final int UPDATEPROGRESS = 3;
	public static final int FINLISH = 4;
	public static final int ERROR = 5;

	@Override
	public boolean handleMessage(Message arg0) {
		switch (arg0.what) {
		case PREPARE:
			mDownCallBack.prepare(url, totalSize);
			break;
		case UPDATEPROGRESS:
			mDownCallBack.updateProgress(url, curPos, totalSize);
			break;
		case FINLISH:
			mDownCallBack.finlish(url, isPause);
			isPause = true;
			break;
		case ERROR:
			mDownCallBack.error(url);
			break;
		}
		return false;
	}
}
